package com.kamm.springbootflower.action;

import com.kamm.springbootflower.domain.Flowertype;
import com.kamm.springbootflower.service.FlowertypeService;
import com.kamm.springbootflower.utils.Message;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Controller
@RequestMapping("/flowertype")
public class flowertypeController {

    @Resource
    FlowertypeService flowertypeService;

    @RequestMapping("/findAll")
    @ResponseBody
    public Map<String,Object> findAll(Flowertype flowertype, @RequestParam(value = "page",required=false) Integer pageNum, @RequestParam(value = "limit",required=false) Integer pageSize){
        Map<String,Object> map=new HashMap<>();
        int totalRows = flowertypeService.count(flowertype);
        List<Flowertype> list= flowertypeService.findAll(flowertype, pageNum,pageSize);
        map.put("code", 0);
        map.put("msg", "");
        map.put("count", totalRows);
        map.put("data", list);
        return map;
    }
    @RequestMapping("/findAll2")
    @ResponseBody
    public Map<String,Object> findAll2(Flowertype flowertype){
        Map<String,Object> map=new HashMap<>();
        int totalRows = flowertypeService.count(flowertype);
        List<Flowertype> list= flowertypeService.findAll2();
        map.put("code", 0);
        map.put("msg", "");
        map.put("count", totalRows);
        map.put("data", list);
        return map;
    }
    @RequestMapping("/addOrUpdate")
    @ResponseBody
    public Message addOrUpdate(Flowertype flowertype) {
        Message msg = new Message();
        if (flowertype.getFtId()!=null&&!"".equals(flowertype.getFtId())) {
           int rows= flowertypeService.update(flowertype);
           if(rows>0){
                msg.setMsg("操作成功!");
                msg.setNum(1);
            }else{
                msg.setNum(0);
                msg.setMsg("操作失败");
            }
        } else {
           int rows= flowertypeService.add(flowertype);
           if(rows>0){
                msg.setMsg("操作成功!");
                msg.setNum(1);
            }else{
                msg.setNum(0);
                msg.setMsg("操作失败");
            }
        }
        return msg;
    }
    @RequestMapping("/findById")
    @ResponseBody
    public  Flowertype findById(Integer ftId){
        Flowertype flowertype=flowertypeService.selectByPrimaryKey(ftId);
       return  flowertype;
    }
    @RequestMapping("/del")
    @ResponseBody
    public Message delete(Flowertype flowertype){
        Message msg = new Message();
        if(flowertype.getFtId()!=null&&!"".equals(flowertype.getFtId())) {
           int rows= flowertypeService.updateByPrimaryKey(flowertype);
            flowertypeService.deleteByPrimaryKey(flowertype.getFtId());
           if(rows>0){
               msg.setMsg("成功");
               msg.setNum(1);
           }else {
               msg.setMsg("失败");
               msg.setNum(0);
           }
        }else {
            msg.setMsg("错误!");
            msg.setNum(3);
        }
        return msg;
    }
    @RequestMapping("/delAll")
    @ResponseBody
    public Message deleteAll(String[] ids){
        Message msg = new Message();
        if(flowertypeService.deleteAll(ids)==0){
            msg.setMsg("失败");
            msg.setNum(0);
        }else {
            msg.setMsg("成功");
            msg.setNum(1);
        }
        return msg;
    }
}

